package com.hd.assistant.dal.dao.ibatis.ordermining;

import java.util.List;

import com.hd.assistant.dal.dao.exception.DAOException;
import com.hd.assistant.dal.dao.orderming.ItemCostsDao;
import com.hd.assistant.dal.dao.query.ordermining.ItemCostQuery;
import com.hd.assistant.dal.dataobject.ordermining.ItemCost;
import com.taobao.common.dao.persistence.DBRoute;
import com.taobao.common.dao.persistence.SqlMapBaseDAO;

public class IbatisItemCostsDao extends SqlMapBaseDAO implements ItemCostsDao{

	public ItemCostQuery getItemCostsByQuery(ItemCostQuery query)
			throws DAOException {
		if(query == null){
			return null;
		}
		
		int totalCount = 0;
		try{
			//获取总的记录数
			if(query.getUserId() != null){
				Long userId = query.getUserId();
				ItemCostQuery countInfo = (ItemCostQuery)this.getItemCostCountByUserId(userId);
				if(countInfo != null){
					query.setAllCount(countInfo.getAllCount());
					query.setCostCount(countInfo.getCostCount());
				}
			}
			
			//获取当前条件下 记录数
			totalCount = (Integer)this.executeQueryForObject("ItemCosts.selectCountsByQuery", query,DBRoute.getOrderMiningRoute());
			query.setTotalCount(totalCount);
			
			@SuppressWarnings("unchecked")
			List<ItemCost>items = (List<ItemCost>)this.executeQueryForList("ItemCosts.selectItemCostsDOsByQuery", query,DBRoute.getOrderMiningRoute());
			query.setItemCosts(items);
		}catch(Exception e){
			throw new DAOException("IbatisItemCostsDao.ItemCostQuery", e);
		}
		
		return query;
	}

	public void insertItemCost(ItemCost itemCost) throws DAOException {
		if(itemCost == null)
			return ;
		try{
			this.executeInsert(
				"ItemCosts.insertItemCostsDO", itemCost,DBRoute.getOrderMiningRoute());
		}catch(Exception e){
			throw new DAOException("IbatisItemCostsDao.insertItemCost", e);
		}
		
	}
	public int updateItemCost(ItemCost itemCost) throws DAOException {
		if(itemCost == null)
			return -1;
		
		try{
			return this.executeUpdate("ItemCosts.updateItemCostsDO", itemCost,DBRoute.getOrderMiningRoute());
		}catch(Exception e){
			throw new DAOException("IbatisItemCostsDao.updateItemCost", e);
		}
		
	}

	public ItemCost getItemCostByItemId(String itemId) throws DAOException {
		if(itemId == null)
			return null;
		
		try{
			return (ItemCost)this.executeQueryForObject("ItemCosts.selectItemCostsDOsByItemid", itemId,DBRoute.getOrderMiningRoute());
		}catch(Exception e){
			throw new DAOException("IbatisItemCostsDao.getItemCostByItemId", e);
		}
	}

	public ItemCostQuery getItemCostCountByUserId(long userId)
			throws DAOException {
		ItemCostQuery countInfo;
		try {
			countInfo = (ItemCostQuery)this.executeQueryForObject("ItemCosts.selectCountByUser", userId,DBRoute.getOrderMiningRoute());
			return countInfo;
		} catch (com.taobao.common.dao.persistence.exception.DAOException e) {
			throw new DAOException("IbatisItemCostsDao.getItemCostCountByUserId", e);
		}
	
	}

	public int batcjhUpdateCost(ItemCostQuery query) throws DAOException {
		if(query == null || query.getCostRatio() == null)
			throw new DAOException("batcjhUpdateCost:query == null || query.getCostRatio() == null");
		try{
			return this.executeUpdate("ItemCosts.updateCostInfo", query,DBRoute.getOrderMiningRoute());
		}catch(Exception e){
			throw new DAOException("IbatisItemCostsDao.updateCostInfo", e);
		}
	}

	public int autoUpdateItemCostSaleInfo(ItemCost itemCost)
			throws DAOException {
		if(itemCost == null || itemCost.getItemId() == null)
			throw new DAOException("autoUpdateItemCostSaleInfo itemCost == null || itemCost.getItemId() == null");
		try{
			return this.executeUpdate("ItemCosts.autoUpdateSaleInfo", itemCost,DBRoute.getOrderMiningRoute());
		}catch(Exception e){
			throw new DAOException("IbatisItemCostsDao.autoUpdateItemCostSaleInfo", e);
		}
	}

	

}
